<?php
add_action('widgets_init', 'register_top_store_home_widget'); // load widget

function register_top_store_home_widget() { // register widget
    register_widget('top_store_home');
}

class top_store_home extends WP_Widget {// widget class {

    function top_store_home() { 
    // widget setting | class name must be same function name
        $widget_ops = array('classname' => 'Lastest news-w', 'description' =>
            'Display top Store Home page');
        $control_ops = array(
            'width' => 250,
            'height' => 350,
            'id_base' => 'top_store_home');
        $this->WP_Widget('top_store_home', 'CouponX - top Stores Home page', $widget_ops, $control_ops);
    }

    function widget($args, $instance) { // display widget
        $title = apply_filters('widget_title', $instance['title']);
        global $wpdb;
        $countItem = $instance['count_item'];
        $cell = $instance['cell'];
        if($cell == ''){
                $cell = 1;
            }
        if($countItem == ''){
                $countItem = 5;
            }
        $li_height = (60+20*($countItem));
        $height = ((70+20*($countItem))*$cell) + 5;
            $my_sql = "
                SELECT term_id as cat_id,term_taxonomy_id as taxonomy_id FROM {$wpdb->term_taxonomy} WHERE taxonomy='store_category' AND parent = 0
            ";
            $categories = $wpdb->get_results($my_sql);
        ?>
            <div class="textlink">
             <div class="row-fluid ti"><span id="down-store"></span><span id="up-store"></span><p><?php echo $title;?></p></div>
             <ul id="linkss-store" data-height="<?php echo $height;?>" class="row-fluid col5 truncated-store" style="height:<?php echo $height.'px';?>">
                 <?php
                    foreach($categories as $categorys){
                        $term_id_parent = $categorys->cat_id;
                        $parent_term = get_term_by('id',$term_id_parent,'store_category');
                        $parent_name = $parent_term->name;
                        $term_children = get_term_children( $term_id_parent, 'store_category' );
                        $str_term_id_child ="";
                        foreach ( $term_children as $child ) {
                            $term = get_term_by( 'id', $child, 'store_category' );
                            $str_term_id_child.=",$term->term_taxonomy_id";
                        }
                    $my_query = "
                            SELECT COUNT(*) as count,meta_value AS store_id FROM {$wpdb->postmeta} 
                            INNER JOIN {$wpdb->posts} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.meta_value)
                            INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id) 
                            WHERE ( {$wpdb->term_relationships}.term_taxonomy_id IN ($categorys->taxonomy_id $str_term_id_child))
                            AND meta_key = 'store_coupon_metadata'
                            GROUP BY meta_value ORDER BY COUNT(*) DESC LIMIT 0,$countItem
                        ";
                   $stores = $wpdb->get_results($my_query);
                   if($stores != null){ ?>
                 <li class="wrap-category" style="height:<?php echo $li_height.'px';?>">
                        <p><?php echo $parent_name;?></p>
                        <ul class="store-name">
                            <?php
                                foreach ($stores as $st){
                                    $store_link = get_permalink($st->store_id);
                                    $store_name = get_the_title($st->store_id);
                            ?>
                                <li>
                                    <a href="<?php echo $store_link;?>" title="<?php echo $store_name;?>"><?php echo $store_name;?></a>
                                </li>
                            <?php }
                            ?>
                        </ul>
                    </li>
               <?php }
                 ?>
                <?php
                    }
                 ?>
             </ul>
             <div class="row-fluid show-store">
                 <span class="more-store">+ more</span>
                 <span class="less-store">- less</span>
             </div>
             </div>
        <?php
    }

    function update($new_instance, $old_instance) { // update widget
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        $instance['count_item'] = $new_instance['count_item'];
        $instance['cell'] = $new_instance['cell'];
        return $instance;
    }

    function form($instance) { // form for the widget options
        ?>
        <div style="color: #333;">
            <p>        
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php echo 'Title'; ?></label>
                <input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
            </p>
            <p>
    		<label for="<?php echo $this->get_field_id('count_item');?>"><?php echo 'Enter number display store in category';?></label>
    		<input type="text" id="<?php echo $this->get_field_id('count_item');?>" name="<?php echo $this->get_field_name('count_item');?>" value="<?php echo $instance['count_item'];?>" style="width:90%;" />
            </p>
            <p>
    		<label for="<?php echo $this->get_field_id('cell');?>"><?php echo 'Enter number cell display';?></label>
    		<input type="text" id="<?php echo $this->get_field_id('cell');?>" name="<?php echo $this->get_field_name('cell');?>" value="<?php echo $instance['cell'];?>" style="width:90%;" />
            </p>
        </div>
        <?php
    }

}
?>
